package com.training1.core.web.controller;


import java.util.ArrayList;


import java.util.List;


import javax.annotation.Resource;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.training1.core.entity.CompanyNameEntity;

import com.training1.core.entity.TodayMoneyEntity;

import com.training1.core.modules.service.CompanyService;


@Controller
@RequestMapping("todayMoney")
public class TodayMoneyController {
	@Resource
	private  CompanyService companyService;
	
	/**
	 * 进入今日应收页面
	 * @return
	 */
	@RequestMapping(value={"/",""})
	public String go(Model model){
		List<TodayMoneyEntity> list=companyService.findAll();
		model.addAttribute("list", list);
		for(int i=0;i<list.size();i++){
			if(list.get(i).getStatus()=="10"||list.get(i).getStatus().equals("10")){
				list.get(i).setStatusStr("逾期中");
			}
			if(list.get(i).getStatus()=="20"||list.get(i).getStatus().equals("20")){
				list.get(i).setStatusStr("执行中");
			}
		}
		return "role/todayMoney";
	}
	
	//模糊查询
	@RequestMapping("find")
	@ResponseBody
	public List<TodayMoneyEntity> find(String customerName,String customerPhone,String recom,String id){
		TodayMoneyEntity TodayMoney=new TodayMoneyEntity();
		TodayMoney.setCustomerName(customerName);
		TodayMoney.setCustomerPhone(customerPhone);
		TodayMoney.setRecom(recom);
		TodayMoney.setUnderCompany(id);
		List<TodayMoneyEntity>list=companyService.findLike(TodayMoney);
		for(int i=0;i<list.size();i++){
			if(list.get(i).getStatus()=="10"||list.get(i).getStatus().equals("10")){
				list.get(i).setStatusStr("逾期中");
			}
			if(list.get(i).getStatus()=="20"||list.get(i).getStatus().equals("20")){
				list.get(i).setStatusStr("执行中");
			}
		}
		return list;
	}
	
	//查询全部的公司名字
	@RequestMapping(value="/companyName",method={RequestMethod.GET})
	@ResponseBody
	public List<CompanyNameEntity> typelist() {
		List<CompanyNameEntity>list = new ArrayList<>();
		CompanyNameEntity s=new CompanyNameEntity();
		//下拉框的开头拼一个全部
		s.setId(null);
		s.setContactName("全部");
		list.add(s);
		list.addAll(companyService.find());
		return list;
	}
	
	//导出表格
	@RequestMapping("export")
	@ResponseBody
	public void export(HttpServletResponse resp,String customerName,String customerPhone,String recom,String id) throws Exception{
		TodayMoneyEntity TodayMoney=new TodayMoneyEntity();
		TodayMoney.setCustomerName(customerName);
		TodayMoney.setCustomerPhone(customerPhone);
		TodayMoney.setRecom(recom);
		TodayMoney.setUnderCompany(id);
		companyService.export(resp, TodayMoney);
	}
	

}
